package com.sxjh.controller.pub;

import com.sxjh.common.BaseController;
import com.sxjh.common.ExcelCacheException;
import com.sxjh.config.AuthContent;
import com.sxjh.config.ExcelCacheConfig;
import com.sxjh.entity.ExcelEntity;
import com.sxjh.entity.User;
import com.sxjh.entity.param.*;
import com.sxjh.service.ExcelService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/excel")
@Api(tags = "财务报表Excel导出")
public class ExcelController extends BaseController {

  @Autowired
  private HttpServletResponse response;

  @Autowired
  private ExcelService excelService;

  @GetMapping("/export/{id}")
  public void export(@PathVariable String id) throws ExcelCacheException{
    try {
      ExcelEntity entity = ExcelCacheConfig.getParam(id);
      // 取出申请该Excel的用户信息并存入该线程
      User user = AuthContent.getUser(entity.getThreadNumber());
      AuthContent.setUser(user);
      if (entity == null) {
        throw new ExcelCacheException("下载链接失效或其他异常，请重试！");
      }
      switch (entity.getType()) {
        case JC:
          excelService.jcExcel((Finance8Param) entity.getData(), response);
          break;
        case JC_MX:
          excelService.jcMxExcel((Finance8List4Param) entity.getData(), response);
          break;
        case BD:
          excelService.reportExcel((ScaleReportParam) entity.getData(), response);
          break;
        case BD_FY:
          excelService.reportBdFyExcel((ScaleReportParam) entity.getData(), response);
          break;
        case LSBB:
          excelService.temporaryTransportExcel((TemporaryTransportParam) entity.getData(), response);
          break;
        case UNLOAD:
          excelService.unloadDetailsExcel((UnloadLogListParam) entity.getData(), response);
          break;
      }
    }catch (Exception e){
      throw new ExcelCacheException("下载链接失效或其他异常，请重试！");
    }
  }
}
